﻿Imports Oracle.DataAccess.Client
Public Class fListaMoviFacturado

    Private lIdDocumento As String
    Private lNumero As Integer
    Private lProducto As String
    Private lImei As String
    Private lIccid As String
    Private lTercero As String
    Private lSucursal As Integer
    Public ReadOnly Property mTipoDoc() As String
        Get
            Return lIdDocumento
        End Get
    End Property
    Public ReadOnly Property mNumeroDoc() As Integer
        Get
            Return lNumero
        End Get
    End Property
    Public ReadOnly Property mProducto() As String
        Get
            Return lProducto
        End Get
    End Property

    Public ReadOnly Property mImei() As String
        Get
            Return lImei
        End Get
    End Property
    Public ReadOnly Property mIccid() As String
        Get
            Return lIccid
        End Get
    End Property


    Public Property mTercero() As String
        Get
            Return lTercero
        End Get
        Set(ByVal value As String)
            lTercero = value
        End Set
    End Property

    Public Property mSucursal() As Integer
        Get
            Return lSucursal
        End Get
        Set(ByVal value As Integer)
            lSucursal = value
        End Set
    End Property

    Private Sub fListaMoviFacturado_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            dtpFechaInicio.Value = dtpFechaFin.Value.AddDays(-30)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub



    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        cmd.CommandText = "SELECT MOV.IDDOCUMENTO,MOV.NUMERO,MOV.FECHADOCUMENTO,MOV.IDBODEGAORIGEN,MOV.IDCANAL,MOV.IDCCOSTO" & _
                          " FROM MOV_MOVIMIENTO MOV,GLO_TIPODOCUMENTO DOC "
        Dim qWhere As String = " WHERE MOV.IDDOCUMENTO = DOC.TIPODOC AND DOC.DOCES=4 "
        Dim CuentaW As Int16 = 1
        If Me.dtpFechaInicio.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= " AND MOV.FECHADOCUMENTO >= '" & FormatDateTime(Me.dtpFechaInicio.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If Me.dtpFechaFin.Text.Trim.Length > 0 Then
            If CuentaW > 0 Then _
               qWhere &= " AND "
            CuentaW += 1
            'qWhere &= "FECHA <= TO_DATE('26012009','DDMMYYYY')"
            qWhere &= "MOV.FECHADOCUMENTO <= '" & FormatDateTime(Me.dtpFechaFin.Value.Date, DateFormat.ShortDate) & "'"
        End If
        If lTercero.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "MOV.IDTERCERO ='" & lTercero.Trim & "'"
        End If
        If lSucursal.ToString.Trim.Length > 0 Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "MOV.SUCURSAL =" & lSucursal & ""
        End If
        'If CuentaW = 0 Then _
        '    qWhere = ""
        cmd.CommandText &= qWhere
        Try
            Conn.Open()
            DsMovimiento.MOV_MOVIMIENTO.Clear()
            DsMovimiento.MOV_MOVIMIENTO.Load(cmd.ExecuteReader)
            Conn.Close()
            MOV_MOVIMIENTODataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub AsignaMovimi(ByVal Fila As Integer)
        Try
            If Fila >= 0 Then
                lIdDocumento = MOV_MOVIMIENTODataGridView.CurrentRow.Cells("IDDOCUMENTO").Value
                lNumero = MOV_MOVIMIENTODataGridView.CurrentRow.Cells("NUMERO").Value
                'lProducto = MOV_MOVIMIENTODataGridView.CurrentRow.Cells("IDPRODUCTO").Value
                'lImei = MOV_MOVIMIENTODataGridView.CurrentRow.Cells("IMEI").Value
                'lIccid = MOV_MOVIMIENTODataGridView.CurrentRow.Cells("ICCID").Value
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    'Private Sub DESCRIPCIONDETALLEDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
    '    AsignaMovimi(e.RowIndex)
    'End Sub



    'Private Sub DESCRIPCIONDETALLEDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    If e.KeyData = Keys.Enter Then
    '        AsignaMovimi(DESCRIPCIONDETALLEDataGridView.CurrentRow.Index)
    '    End If
    'End Sub


    Private Sub btnBuscaProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProducto.Click
        Try
            Dim flprod As New fListaProductos
            If flprod.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtProducto.Text = flprod.idArticulo
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MOV_MOVIMIENTODataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MOV_MOVIMIENTODataGridView.CellMouseDoubleClick
        AsignaMovimi(e.RowIndex)
    End Sub



    Private Sub MOV_MOVIMIENTODataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_MOVIMIENTODataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaMovimi(MOV_MOVIMIENTODataGridView.CurrentRow.Index)
        End If
    End Sub
End Class